#include "measurepage_p1.h"
#include "ui_measurepage_p1.h"
#include "Beans/userinfobeab.h"
#include "measureresultpage.h"
#include "Config/constant.h"
#include "dialog/myhelper.h"
#include "QMediaPlayer"
#include <QAudioOutput>
#include "others/wmic.h"

MeasurePage_P1::MeasurePage_P1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MeasurePage_P1)
{
    ui->setupUi(this);
    setBg();
    if(Constant::ShowFullScreen)
        this->showFullScreen();
    InitialChartView();
    InitialControl();
//    uid=new UserInfoInputDialog();
//    uid->setModal(true);
//    connect(this,SIGNAL(Initial()),uid,SLOT(Initial()));//用于清空用户信息

   // connect(uid,SIGNAL(SendUserInfo(UserInfoBeab)),this,SLOT(getUserInfoFromDialog(UserInfoBeab)));

    if(Constant::SoftWare_Version==1)
    {
        threadWork=new ThreadWork();
        threadWork->start();
        connect(threadWork,SIGNAL(sendData(double,double,double,double)),this,SLOT(getPPGData(double,double,double,double)));
        connect(threadWork,SIGNAL(sendHR2UI(unsigned char,int)),this,SLOT(getHRData(unsigned char,int)));
        connect(threadWork,SIGNAL(sendSPOX2UI(double,int)),this,SLOT(getSPOXData(double,int)));
        connect(threadWork,SIGNAL(sendRR2UI(int,int)),this,SLOT(getRRData(int,int)));
        connect(threadWork,SIGNAL(sendProgress2UI(int,int)),this,SLOT(getProgressData(int,int)));
        connect(threadWork,SIGNAL(sendOver(int)),this,SLOT(getOver(int)));

        connect(this,SIGNAL(sendStartOrStop(bool,int)),threadWork,SLOT(getStartOrStop(bool,int)));
        connect(this,SIGNAL(sendUserInfo2Thread(UserInfoBeab,int)),threadWork,SLOT(getUserInfo(UserInfoBeab,int)));

        QCoreApplication::setOrganizationName(QString("QXZD"));
        QCoreApplication::setApplicationName(QString("Qt"));
        QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
        WMIC wmic;
        QString id=wmic.getCpuId();

        //    ui->soft_regist_lineEdit->setText(id);
        qDebug()<<"id"<<id;
        qDebug()<<"settings="<<settings.value("Name").toString();
        QString id_r=wmic.MachineCodetoIDNumber(settings.value("Name").toString());
        QString mc=wmic.IDNumbertoMachineCode(id);

        if(id!=id_r){
            //未注册
            myHelper::ShowRegistInfo();
        }
    }
    mrp=new MeasureResultPage();
    connect(this,SIGNAL(SendUserInfoToResult(UserInfoBeab)),mrp,SLOT(getUserInfoFromMeasure(UserInfoBeab)));
    mrp->hide();
    pos=0;
    myplayer=new QMediaPlayer();
    audioOutput=new QAudioOutput();
    audioOutput->setVolume(100);
    myplayer->setAudioOutput(audioOutput);
    myplayer->setSource(QUrl("qrc:/sound/info.mp3"));


}

MeasurePage_P1::~MeasurePage_P1()
{
    delete audioOutput;
    delete myplayer;

    delete ppg_Chart;
    delete appg_Chart;
    delete hrv_Chart;
    if(Constant::SoftWare_Version==1)
    {
        delete threadWork;
    }
    qDebug()<<"~MeasurePage_P1";
    delete ui;
}

/**
 * @brief MeasurePage_P1::InitialChartView
 * 功能：初始化波形图
 */
void MeasurePage_P1::InitialChartView()
{
    ppg_Chart=MPC.CreateLineChart_PPG1();
    ui->ChartView_PPG->setChart(ppg_Chart);
    ui->ChartView_PPG->setRenderHint(QPainter::Antialiasing, true);//设置无锯齿

    appg_Chart=MPC.CreateLineChart_APPG1();
    ui->ChartView_APPG->setChart(appg_Chart);
    ui->ChartView_APPG->setRenderHint(QPainter::Antialiasing, true);//设置无锯齿

    hrv_Chart=MPC.CreateHRVHistBarChart();
    ui->ChartView_HRV->setChart(hrv_Chart);
    ui->ChartView_HRV->setRenderHint(QPainter::Antialiasing, true);//设置无锯齿
}

/**
 * 功能：设置背景图片
 * @brief WelcomePage::setBg
 */
void MeasurePage_P1::setBg()
{
    QPalette pal = this->palette();
    pal.setBrush(QPalette::Window,QBrush(QPixmap(":/images/MeasurePage/measure_bg.png")));
    setPalette(pal);
}
/**
 * 功能：用于清空波形图表和指标值
 * @brief MeasurePage_P1::Initial
 */
void MeasurePage_P1::InitialControl()
{
    ui->Mesure_label_ID_V->setText("");
    ui->Mesure_label_Name_V->setText("");
    ui->label_hr_v->setText("--");
    ui->label_ox_v->setText("--");
    ui->progressBar->setValue(0);
    ui->bt_Start->setVisible(true);
}

void MeasurePage_P1::on_bt_Start_clicked()
{

//    emit Initial();
//    uid->show();
    this->CurrentUser=myHelper::ShowInfo();
//    this->CurrentUser=uib;

    ui->Mesure_label_ID_V->setText(CurrentUser.UserID);
    ui->Mesure_label_Name_V->setText(CurrentUser.UserName);
    qDebug()<<CurrentUser.UserSex;
    if(CurrentUser.UserID.length()>0){
        emit sendUserInfo2Thread(CurrentUser,1);
        emit sendStartOrStop(true,1);
        ui->bt_Start->setEnabled(false);
        myplayer->play();
        ui->bt_Start->setVisible(false);
    }else
    {
        ui->bt_Start->setEnabled(true);
        emit sendStartOrStop(false,1);
    }
    pos=0;
}


void MeasurePage_P1::on_bt_End_clicked()
{
    if(myHelper::ShowMessageBoxQuesion("您确认退出吗？")==1){
        emit sendStartOrStop(false,1);
        ui->bt_Start->setEnabled(true);
        for(int i=0;i<2500;i++)
        {
            MPC.Update_APPGData1(0);
            MPC.Update_PPGData1(0);
        }

        for(int i=0;i<30;i++)
            MPC.Update_HRVHistBarData(i,0);
        this->close();
    }


}

void MeasurePage_P1::getPPGData(double ppg1,double appg1,double ppg2,double appg2)
{
    MPC.Update_PPGData1(ppg1);
    MPC.Update_APPGData1(appg1);
}
void MeasurePage_P1::getHRData(unsigned char hr,int p)
{
    if(hr==0)
        pos=0;
    if(p==1)
        ui->label_hr_v->setText(QString::number(hr));
}
void MeasurePage_P1::getSPOXData(double spox,int p)
{
    if(spox==0)
        pos=0;
    if(p==1)
        ui->label_ox_v->setText(QString::number(spox,'f',1));
}
void MeasurePage_P1::getRRData(int RR,int p)
{
    if(p==1){
        MPC.Update_HRVHistBarData(pos++,RR);
        if(pos>30)
            pos=0;
    }
}
void MeasurePage_P1::getProgressData(int p,int persion)
{
    if(persion==1)
        ui->progressBar->setValue(p);

}
void MeasurePage_P1::getOver(int p)
{
    if(p==1){
        mrp->show();
        ui->bt_Start->setEnabled(true);
        for(int i=0;i<2500;i++)
        {
            MPC.Update_APPGData1(0);
            MPC.Update_PPGData1(0);
        }

        for(int i=0;i<30;i++)
            MPC.Update_HRVHistBarData(i,0);

        emit SendUserInfoToResult(CurrentUser);
        InitialControl();
        this->close();
    }

}
void MeasurePage_P1::getUserInfoFromDialog(UserInfoBeab uib)
{
    this->CurrentUser=uib;
    emit sendUserInfo2Thread(CurrentUser,1);
    ui->Mesure_label_ID_V->setText(uib.UserID);
    ui->Mesure_label_Name_V->setText(uib.UserName);
    qDebug()<<uib.UserSex;
    if(uib.UserID.length()>0){
        emit sendStartOrStop(true,1);
        ui->bt_Start->setEnabled(false);
        myplayer->play();
        ui->bt_Start->setVisible(false);
    }else
    {
        ui->bt_Start->setEnabled(true);
        emit sendStartOrStop(false,1);
    }
//    uid->hide();

}
